﻿using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using PhoenixCrabV6.Engine.Screens;

namespace PhoenixCrabV6.Engine.IO
{
    public class InputManager
    {
        private KeyboardState currentKeyState, prevKeyState;
        private MouseState currentMouseState, prevMouseState;

        private static InputManager instance;

        public static InputManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new InputManager();
                return instance;
            }
        }

        public void Update()
        {
            prevKeyState = currentKeyState;
            currentKeyState = Keyboard.GetState();

            prevMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();


        }

        public bool KeyPressed(params Keys[] keys)
        {
            return keys.Any(key => currentKeyState.IsKeyDown(key) && prevKeyState.IsKeyUp(key));
        }

        public bool KeyReleased(params Keys[] keys)
        {
            return keys.Any(key => currentKeyState.IsKeyUp(key) && prevKeyState.IsKeyDown(key));
        }

        public bool KeyDown(params Keys[] keys)
        {
            return keys.Any(key => currentKeyState.IsKeyDown(key));
        }

        public bool LeftClick()
        {
            return (currentMouseState.LeftButton == ButtonState.Pressed &&
                    prevMouseState.LeftButton == ButtonState.Released);
        }

        public bool RightClick()
        {
            return (currentMouseState.RightButton == ButtonState.Pressed &&
                    prevMouseState.RightButton == ButtonState.Released);
        }

        public bool MiddleClick()
        {
            return (currentMouseState.MiddleButton == ButtonState.Pressed &&
                    prevMouseState.MiddleButton == ButtonState.Released);
        }

        public bool IsClick()
        {
            return LeftClick() || RightClick() || MiddleClick();
        }

        public Vector2 CurrentMousePosition()
        {
            return new Vector2(currentMouseState.X, currentMouseState.Y);
        }

    }
}